package com.ztjava.pay.validator;

import java.util.Date;

import com.jfinal.aop.Duang;
import com.jfinal.core.Controller;
import com.xiaoleilu.hutool.date.DateUtil;
import com.ztjava.core.Constant;
import com.ztjava.core.base.BaseValidator;
import com.ztjava.pay.model.PayOrder;
import com.ztjava.pay.service.PayService;

public class PayValidator extends BaseValidator{
	private static PayService payService = Duang.duang(PayService.class);
	
	@Override
	protected void validate(Controller c) {
		setShortCircuit(true);
		String num = c.getPara("num","");
		PayOrder payOrder = payService.getOrderByNum(num);
		if(payOrder==null){
			addError(Constant.MESSAGE, "订单不存在！");
		}
		if(payOrder.getStr("status").equals("CANCEL")){
			addError(Constant.MESSAGE,"已取消支付！");
		}
		if(payOrder.getStr("status").equals("PAID")){
			addError(Constant.MESSAGE,"请勿重复支付！");
		}
		if(payOrder.getTimestamp("create_time").getTime()<DateUtil.offsetMinute(new Date(), -30).getTime()){
			addError(Constant.MESSAGE,"订单信息已过期！");
		}
		c.setAttr("payOrder", payOrder);
	}
}
